﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Design_Pattners.Composite;

namespace Module_Basic_Entities
{
    /// <summary>
    /// Composite
    /// </summary>
    public abstract class Map : MapComponent, IComposite
    {
        /// <summary>
        /// Represents a map from the Mappy Tool.
        /// </summary>
        public Map()
        {
            
        }

        public override void Init()
        {
            if (_children == null)
                _children = new List<IComponent>();
            else
                _children.Clear();

            foreach (MapComponent c in this._children)
            {
                c.Init();
            }
        
        }

        public Layer GetLayerAt(int index)
        {
            if (index > this.NumChildLayers)
            {
                return null;
            }

            Layer layer = null;

            int i = 0;
            
            foreach (MapComponent c in this._children)
            {
                if (c is Layer)
                {
                    if (i == index)
                    {
                        layer = (Layer)c;
                        break;
                    }

                    i++;
                }
                else
                {
                    layer = this.GetLayerAt((Map)c, index, ref i);
                    
                    if(layer != null)
                        break;
                }

            }

            return layer;
        }

        private Layer GetLayerAt(Map map, int index, ref int curIndex)
        {
            Layer layer = null;

            foreach (MapComponent c in map._children)
            {
                if (c is Layer)
                {
                    curIndex++;
                    if (curIndex-1 == index)
                    {
                        layer = (Layer)c;
                        break;
                    }
                }                
            }

            return layer;
        }

        #region IComposite Members        

        public List<IComponent> _children
        {
            get;
            private set;
        }


        public override int NumChildLayers
        {
            get
            {
                int x = 0;

                foreach (MapComponent c in this._children)
                {
                    x += c.NumChildLayers;
                }

                return x;
            }
            set
            {
                base.NumChildLayers = value;
            }
        }
        public override void Add<MapComponent>(MapComponent c)
        {
            c.Init();            

            this._children.Add(c);
        }

        public override void Remove<MapComponent>(MapComponent c)
        {
            this._children.Remove(c);
        }
        #endregion

        public override void Draw(float gameTime)
        {
            // Recursively display child nodes
            foreach (MapComponent component in this._children)
            {
                component.Draw(gameTime);
            }
        }

      
    }
}
